//
//  SMSaveMoneyView.m
//  SaveMoney
//
//  Created by user on 2018/11/22.
//  Copyright © 2018年 Sunflower. All rights reserved.
//

#import "SMSaveMoneyView.h"


#define KViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]

@interface SMSaveMoneyView ()

@property (strong, nonatomic) IBOutlet UIView *contentView;

@property (weak, nonatomic) IBOutlet UIView *containView;


@end

@implementation SMSaveMoneyView

+ (instancetype)showViewWithView:(UIView *)View {
    
    UIWindow *windows = [UIApplication sharedApplication].keyWindow;
    
    SMSaveMoneyView *view = [[SMSaveMoneyView alloc] initWithFrame:View.bounds];
    
    [View addSubview:view];
    
    return view;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.contentView = [[[NSBundle mainBundle] loadNibNamed:@"SMSaveMoneyView" owner:self options:nil] lastObject];
        self.contentView.frame = self.bounds;
        [self addSubview:self.contentView];
        
        KViewBorderRadius(self.containView, SBScaleNumber(20), SBScaleNumber(1), [UIColor hexStringToColor:@"0096FF"]);
        KViewBorderRadius(self.sendMoneyLabel, SBScaleNumber(10), SBScaleNumber(1), [UIColor hexStringToColor:@"0096FF"]);
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissaction)];
        [self.contentView addGestureRecognizer:tap];
    }
    return self;
}

- (void)disMiss:(BOOL)isButton {
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.5 animations:^{
        if (isButton) {
            weakSelf.containView.transform = CGAffineTransformMakeScale(0.01, 0.01);
            weakSelf.alpha = 0;
        } else {
            weakSelf.alpha = 0;
        }
        
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeView];
        }
    }];
}

- (IBAction)saveMoneyAction:(id)sender {
    if ([_sendMoneyLabel.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请输入金额"];
    }
    NSInteger money = [_sendMoneyLabel.text integerValue];
    if (self.block) {
        self.block(sender, money, _messageText.text);
    }
    [self disMiss:YES];
}

- (void)disMissaction {
    [self disMiss:NO];
}

- (void)removeView {
    [self.sendMoneyLabel resignFirstResponder];
    [self removeFromSuperview];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
